from PyQt6.QtCore import QSize
from PyQt6.QtGui import QFont
from PyQt6.QtWidgets import QHBoxLayout, QLabel, \
    QWidget, QSizePolicy, QVBoxLayout, QPushButton

from scripts.beans import Storyboard
from ui.widgets.CustomWidgets import MyCheckBox


class BoardAdapterItem():

    def setSelected(self, select):
        self.index_checkbox.setChecked(select)

    def __init__(self, itemwidget, index, bean: Storyboard, callback):
        super().__init__()
        self.index = index
        self.itemwidget = itemwidget
        self.bean = bean
        self.images_widget: QWidget = None
        self.itemview: QHBoxLayout = None
        self.initView(index, itemwidget)
        self.callback = callback

    def initView(self, index, itemwidget):
        itemwidget.setSizePolicy(QSizePolicy.Policy.Preferred, QSizePolicy.Policy.Expanding)

        self.itemview = QHBoxLayout()
        self.itemview.setSpacing(4)
        self.itemview.setContentsMargins(2, 2, 2, 2)
        itemwidget.setLayout(self.itemview)

        # index
        self.index_checkbox = MyCheckBox(str(index))
        self.add_itemView(self.index_checkbox, self.itemview, 1, QSizePolicy.Policy.Minimum)

        # 合并按钮
        self.mergeview = QWidget()
        self.mergelayout = QVBoxLayout()
        self.mergelayout.setContentsMargins(2, 2, 2, 2)
        self.mergeview.setLayout(self.mergelayout)
        self.mergelayout.setSpacing(4)
        self.add_itemView(self.mergeview, self.itemview, 1, QSizePolicy.Policy.Minimum)
        self.mergeup_btn = QPushButton('向上合并')
        self.mergelayout.addWidget(self.mergeup_btn)
        self.mergeup_btn.clicked.connect(lambda: self.callback(1, self.index))
        self.split_down_btn = QPushButton('拆开分镜')
        self.mergelayout.addWidget(self.split_down_btn)
        self.split_down_btn.clicked.connect(lambda: self.callback(3, self.index))
        self.merge_down_btn = QPushButton('向下合并')
        self.mergelayout.addWidget(self.merge_down_btn)
        self.merge_down_btn.clicked.connect(lambda: self.callback(2, self.index))


        # 字幕
        self.srtview = QWidget()
        self.srtlayout = QVBoxLayout()
        self.srtlayout.setContentsMargins(6, 4, 6, 4)
        self.srtview.setLayout(self.srtlayout)
        self.srtlayout.setSpacing(4)
        self.add_itemView(self.srtview, self.itemview, 10, QSizePolicy.Policy.Minimum)
        for srt in self.bean.srtbean:
            srt_tv = QLabel()
            font = QFont('Arial',14,100)
            srt_tv.setText(srt['text'] if srt['text'] is not None else "无字幕")
            srt_tv.setWordWrap(True)
            srt_tv.setSizePolicy(QSizePolicy.Policy.Preferred, QSizePolicy.Policy.Preferred)
            srt_tv.setFont(font)
            self.srtlayout.addWidget(srt_tv)

        # 时间轴
        time_tv = QLabel(f"开始：{self.bean.start}\n结束：{self.bean.end}")
        self.add_itemView(time_tv, self.itemview, 1, QSizePolicy.Policy.Preferred, width=100)

    def add_itemView(self, view: QWidget, parent, factor, policy=QSizePolicy.Policy.Expanding, width=40):
        view.setMinimumHeight(40)
        view.setMinimumWidth(width)

        view.setContentsMargins(0, 0, 0, 0)
        # view.setStyleSheet("background-color: rgba(255, 255, 255, 255)")
        view.setSizePolicy(policy, QSizePolicy.Policy.Expanding)
        parent.addWidget(view, stretch=factor)
